﻿using System;
using System.Collections.Generic;
using System.Text;
using BF.IBLL.BaseSystem;
using BF.Infrastructure;
using BF.IDAL.BaseSystem;
using BF.Model;
using System.Linq.Expressions;
using System.Linq;
using BF.Membership.MembershipEF;
using BF.Membership.BLL;

namespace BF.BLL.BaseSystem
{
    public class Sys_FunctionBLL : Isys_FunctionBLL
    {
        private readonly Isys_FunctionDAL iDal;
        private readonly Isys_PermissionDAL iPermissionDAL;
        private readonly IUnitOfWork<WSJD_BaseFrameworkEntities> uwork;
        public Sys_FunctionBLL(
            IUnitOfWork<WSJD_BaseFrameworkEntities> uwork, 
            Isys_FunctionDAL iDal, 
            Isys_PermissionDAL iPermissionDAL)
        {
            this.iDal = iDal;
            this.uwork = uwork;
            this.iPermissionDAL = iPermissionDAL;
        }

        #region Isys_FunctionBLL 成员

        public bool Add(Model.sys_Function entity)
        {
            AspnetRolesBLL roleBLL = new AspnetRolesBLL();
            iDal.Add(entity);
            aspnet_Roles aspnetRoles = roleBLL.GetModelByName("系统管理员");
            sys_Permission sysPermission = new sys_Permission();
            sysPermission.gKey = Guid.NewGuid();
            sysPermission.gRoleID = aspnetRoles.RoleId;
            sysPermission.gFunctionKey = entity.gKey;
            iPermissionDAL.Add(sysPermission);
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        public bool Delete(Model.sys_Function entity)
        {
            iDal.Delete(entity);
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        public bool Delete(Expression<Func<Model.sys_Function, bool>> predicate)
        {
            iDal.Delete(predicate);
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        public bool Delete(List<string> keyList)
        {
            foreach (var item in keyList)
            {
                sys_Function model = iDal
                    .GetModelByCondition(P => P.gKey.Equals(new Guid(item)));
                List<sys_Function> list = iDal
                    .GetList(P => P.gParentKey == model.gKey);
                if (list.Count > 0)
                    DeleteList(list);
                iDal.Delete(model);
                iPermissionDAL.Delete(P => P.gFunctionKey == model.gKey);
            }
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        private void DeleteList(List<sys_Function> list)
        {
            foreach (var item in list)
            {
                List<sys_Function> tmpList = iDal
                    .GetList(P => P.gParentKey == item.gKey);
                if (list.Count > 0)
                    DeleteList(tmpList);
                iDal.Delete(item);
                iPermissionDAL.Delete(P => P.gFunctionKey == item.gKey);
            }
        }

        public sys_Function GetModelByID(string guid)
        {
            return iDal
                .GetModelByCondition(P => P.gKey.Equals(new Guid(guid)));
        }

        public List<sys_Function> GetList(
            Expression<Func<sys_Function, bool>> where, 
            Expression<Func<sys_Function, object>> orderBy, 
            PageModel pageModel, 
            bool isAscending)
        {
            return iDal.GetList(where, orderBy, pageModel, isAscending);
        }

        public List<sys_Function> GetList(Expression<Func<sys_Function, bool>> where)
        {
            return iDal.GetList(where);
        }

        public int GetRowCount(Expression<Func<sys_Function, bool>> where)
        {
            return iDal.GetRowCount(where);
        }

        public bool Update()
        {
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        public string Up(string gKey, int iSort)
        {

            sys_Function model = GetModelByID(gKey);
            if (iSort > 0)
            {
                int? newSort = iDal
                  .GetList(P => P.gParentKey == model.gParentKey && P.iSort < iSort)
                  .Max(P => P.iSort);
                if (newSort == null)
                    return "已是第一条,无法向上!";
                sys_Function upModel = iDal
                    .GetModelByCondition(P => P.gParentKey == model.gParentKey 
                        && P.iSort == newSort);
                model.iSort = newSort;
                upModel.iSort = iSort;
                if (uwork.Commit() > 0)
                    return "操作成功!";
                else
                    return "操作失败!";
            }
            return "已是第一条,无法向上!";
        }

        public string Down(string gKey, int iSort)
        {
            sys_Function model = GetModelByID(gKey);
            int? newSort = iDal
                  .GetList(P => P.gParentKey == model.gParentKey && P.iSort > iSort)
                  .Min(P => P.iSort);
            if (newSort != null)
            {
                sys_Function upModel = iDal
                    .GetModelByCondition(P => P.gParentKey == model.gParentKey 
                        && P.iSort == newSort);

                model.iSort = newSort;
                upModel.iSort = iSort;
                if (uwork.Commit() > 0)
                    return "操作成功!";
                else
                    return "操作失败!";
            }
            return "已是最后一条,无法向下!";
        }

        #endregion
    }
}
